/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.umss.arqui2010;

import edu.umss.arqui10.modelo.Account;
import edu.umss.arqui10.modelo.Holder;
import edu.umss.arqui10.modelo.OutputBean;
//import edu.umss.arqui10.modelo.Registry;
import edu.umss.arqui10.modelo.Responsible;
import edu.umss.arqui10.modelo.Service;
import edu.umss.arqui10.modelo.ServiceReceiver;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.TreeMap;

/**
 *
 * @author Ninten
 */
public class JAlgo {
    //Rho

    private TreeMap<Long, Service> services;
    private TreeMap<Long, ServiceReceiver> serviceReceivers;
    private LinkedList<ServiceReceiver> serviceReceiverList;
    private LinkedList<OutputBean> outputBeans;
    private LinkedList<Holder> holders;
    private LinkedList<Account> accounts;
    private TreeMap<Integer, Responsible> responsibleList;

    public JAlgo() {
        services = new TreeMap<Long, Service>();
        outputBeans = new LinkedList<OutputBean>();
    }

    public OutputBean addOutputBean(String name, String description, Long id, BigDecimal price) {
        //guardar
        OutputBean ob = new OutputBean(name, description, id, price);
        outputBeans.add(ob);
        return ob;
    }

    public OutputBean getOutputBean(Long id) {
        for (OutputBean ob : outputBeans) {
            if (ob.getId().equals(id)) {
                return ob;
            }
        }
        return null;
    }

    public boolean changeCurrentPrice(Long id, BigDecimal price) {
        OutputBean ob = getOutputBean(id);
        if (ob != null) {
            ob.changeCurrentPrice(price);
            return true;
        }
        return false;
    }

    public boolean changeName(Long id, String name) {
        OutputBean ob = getOutputBean(id);
        if (ob != null) {
            ob.setName(name);
            return true;
        }
        return false;
    }

    public boolean changeDescription(Long id, String description) {
        OutputBean ob = getOutputBean(id);
        if (ob != null) {
            ob.setDescription(description);
            return true;
        }
        return false;
    }

    public boolean setUsable(Long id, boolean enable) {
        OutputBean ob = getOutputBean(id);
        if (ob != null) {
            ob.setUsable(enable);
            return true;
        }
        return false;
    }

    //Rho
    public void addService(Long id, String name, String desc, BigDecimal t) {
        Service newService = new Service(id, name, desc, t);
        services.put(id, newService);
    }

    //Rho
    public Collection getServices() {
        return this.services.values();
    }

    //Rho
    public void removeService(Long id) {
        this.services.get(id).setUsable(false);
    }

    public void assignServiceAt(Long idRS, Long idServ) {
        ServiceReceiver rs = this.serviceReceivers.get(idRS);
        Service service = this.services.get(idServ);
        //TODO
        //rs.assingService(service);
    }
    public ServiceReceiver addServiceReceiver(String name) {

        ServiceReceiver serviceReceiver = null;

        Iterator<ServiceReceiver> it = serviceReceiverList.iterator();
        while (it.hasNext()) {
            ServiceReceiver sr = it.next();
            if (sr.getName().equalsIgnoreCase(name)) {
                serviceReceiver = sr;
            }
        }
        if (serviceReceiver == null) {
            serviceReceiver = new ServiceReceiver(name);
        }
        return serviceReceiver;
    }

    public LinkedList<ServiceReceiver> findServiceReceiversByName(String name) {
        LinkedList<ServiceReceiver> list = new LinkedList<ServiceReceiver>();
        Iterator<ServiceReceiver> it = serviceReceiverList.iterator();
        while (it.hasNext()) {
            ServiceReceiver sr = it.next();
            if (sr.getName().equalsIgnoreCase(name)) {
                list.add(sr);
            }
        }
        return list;
    }

    public Holder newHolder(Long id) {
        Holder h = null;
        for (int i = 0; i < holders.size(); i++) {
            Holder holder = holders.get(i);
            if (holder.getId() == id) {
                h = holder;
            }
        }
        if (h == null) {
            h = new Holder(id);
        }
        return h;
    }

    public Holder getHolderById(Long id) {
        Holder h = null;
        for (int i = 0; i < holders.size(); i++) {
            Holder holder = holders.get(i);
            if (holder.getId() == id) {
                h = holder;
            }
        }
        return h;
    }

    public Holder getHolderByName(String name) {
        Holder h = null;

        for (int i = 0; i < holders.size(); i++) {
            Holder holder = holders.get(i);
            if (holder.getName().equalsIgnoreCase(name)) {
                h = holder;
            }
        }
        return h;
    }

    public Account getAccountByHolder(Holder h) {
        return h.getAccount();
    }

    public void removeHolder(String name) {
        for (int i = 0; i < holders.size(); i++) {
            Holder holder = holders.get(i);
            if (holder.getName().equalsIgnoreCase(name)) {
                holder = null;
            }
        }
    }
    
   
    public Responsible getResponsibleById(Integer id){
        return responsibleList.get(id);
    }

    public Responsible getResponsibleByName(String name){
        Collection c = responsibleList.values();
        Iterator<Responsible> it = c.iterator();
        Responsible res = null;
        while(it.hasNext()){
            res = ((Responsible)it.next());
            if(res.getName().equals(name)){
                break;
            }
        }
        if(res==null){
            throw new UnsupportedOperationException("Responsible not found (ByName)");
        }
        return res;
    }
    public void addResponsible(String ci, String name, String address, String phone, String responsability, boolean active) {
        Responsible responsible = new Responsible(name, ci, responsability);

        responsible.setAddress(address);
        responsible.setPhone(phone);
        responsible.setActive(active);

        responsibleList.put(Integer.parseInt(ci), responsible);
    //y luego hacemos la consulta a la base de datos para la insercion de este responsable
    }
    public List<Responsible> findResponsiblesByName(String name) {
        List<Responsible> results = new LinkedList<Responsible>();
        //aqui deberia entrar el codigo de hibernate donde se hace la consulta a la BD
        return results;
    }

    public Responsible findResponsibleByCi(List<Responsible> ListResponsibles, String ci) {
        Responsible res = null;
        Iterator<Responsible> iterator = ListResponsibles.iterator();
        Responsible current = null;

        while (iterator.hasNext()) {
            current = iterator.next();

            if (current.getCi().equals(ci)) {
                res = current;
                break;
            }
        }

        return res;
    }

    public void insertResponsible(String ci, String name, String address, String phone, String responsability, boolean active){
        List<Responsible> responsiblesByName = findResponsiblesByName(name);
        Responsible responsibleByCi = findResponsibleByCi(responsiblesByName, ci);

        if(responsibleByCi == null){
            addResponsible(ci, name, address, phone, responsability, active);
        }
        else{
            System.out.println("this responsible is already exist");
        }
    }
}
